﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class MetaDataQuoteDao : DaoService<MetaDataQuote>
    {
        public MetaDataQuoteDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "FAS_ProjectMetaDataQuote";
            }
        }       

        public MetaDataQuote GetMetaDataQuote(string metaDataID, string projectBranchID, string targetProjectBranchID)
        {
            var sqlCondition = "(ProjectTreeID=@ProjectTreeID Or QuoteMetaDataID=@ProjectTreeID)";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@ProjectTreeID", TDbDataType.VarChar, metaDataID)
            };
            if(string.IsNullOrEmpty(projectBranchID) == false)
            {
                sqlCondition = string.Format(@"{0} AND ProjectBranchID=@ProjectBranchID ", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchID));
            }
            if (string.IsNullOrEmpty(targetProjectBranchID) == false) {
                sqlCondition = string.Format(@"{0} AND not exists (select 1 from fas_projectmetadataquote where ProjectBranchID=@TargetProjectBranchID AND quote.QuoteMetaDataID=QuoteMetaDataID)", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@TargetProjectBranchID", TDbDataType.VarChar, targetProjectBranchID));
            }
            var ds = this.DB.ExecuteDataSet(string.Format("SELECT ID,ProjectID,ProjectBranchID,ProjectTreeID,QuoteProductLineID,QuoteProductID,QuoteProductVersionID,QuoteSubSystemID,QuoteProjectID,QuoteProjectBranchID,QuoteMetaDataID,QuoteMetaDataVersionID,QuoteMetaDataVersion,QuoteType,Description,Creator,CreateTime,LastModifier,LastModifyTime FROM {0} quote WHERE {1} ", TableName, sqlCondition), paramsList.ToArray());
            MetaDataQuote quote = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                quote = this.ConvertToObject(this.Columns, row);
            }
            return quote;
        }
        public List<MetaDataQuote> QueryProjectQuoteMetaDataList(string branchId, string quoteProjectBranchID)
        {
            var sqlCondition = "ProjectBranchID=@ProjectBranchID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, branchId)
            };
            if (string.IsNullOrEmpty(quoteProjectBranchID) == false) {
                sqlCondition = string.Format("{0} AND QuoteProjectBranchID=@QuoteProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, quoteProjectBranchID));
            }
            var ds = this.DB.ExecuteDataSet(string.Format("SELECT ID,ProjectID,ProjectBranchID,ProjectTreeID,QuoteProductLineID,QuoteProductID,QuoteProductVersionID,QuoteSubSystemID,QuoteProjectID,QuoteProjectBranchID,QuoteMetaDataID,QuoteMetaDataVersionID,QuoteMetaDataVersion,QuoteType,Description,Creator,CreateTime,LastModifier,LastModifyTime FROM {0} WHERE {1} ", TableName, sqlCondition), paramsList.ToArray());
            List<MetaDataQuote> list = new List<MetaDataQuote>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows) {
                    list.Add(this.ConvertToObject(this.Columns, row));
                }
            }
            return list;
        }

        public List<MetaDataQuote> GetDependencyProjectQuoteMetaDataList(string quoteMetaDataID, string quoteProjectBranchID) {
            //var sqlCondition = @"QuoteProjectBranchID=@QuoteProjectBranchID AND QuoteMetaDataID in (( select ParentMetaDataID from meta_dependency where ParentMetaDataID=@QuoteMetaDataID and QuoteProjectBranchID=@QuoteProjectBranchID))";
            var sqlCondition = @"QuoteProjectBranchID=@QuoteProjectBranchID AND QuoteMetaDataID=@QuoteMetaDataID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, quoteProjectBranchID),
                this.DB.MakeInParam("@QuoteMetaDataID", TDbDataType.VarChar, quoteMetaDataID)
            };
            var ds = this.DB.ExecuteDataSet(string.Format("SELECT ID,ProjectID,ProjectBranchID,ProjectTreeID,QuoteProductLineID,QuoteProductID,QuoteProductVersionID,QuoteSubSystemID,QuoteProjectID,QuoteProjectBranchID,QuoteMetaDataID,QuoteMetaDataVersionID,QuoteMetaDataVersion,QuoteType,Description,Creator,CreateTime,LastModifier,LastModifyTime FROM {0} WHERE {1} ", TableName, sqlCondition), paramsList.ToArray());
            List<MetaDataQuote> list = new List<MetaDataQuote>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(this.ConvertToObject(this.Columns, row));
                }
            }
            return list;

        }
        public void DeleteProjectQuoteMetaData(string branchId, string quoteProjectBranchID, string metaDataID) {
            var sqlCondition = "ProjectBranchID=@ProjectBranchID AND QuoteMetaDataID=@QuoteMetaDataID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, branchId),
                this.DB.MakeInParam("@QuoteMetaDataID", TDbDataType.VarChar, metaDataID),
            };
            if (string.IsNullOrEmpty(quoteProjectBranchID) == false)
            {
                sqlCondition = string.Format("{0} AND QuoteProjectBranchID=@QuoteProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, quoteProjectBranchID));
            }
            this.DB.ExecSqlStatement(string.Format("DELETE FROM {0} WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
        }

        public void UpdateProjectQuoteMetaData(string branchId, string quoteBranchID, string metaDataID, string versionID, string modifier) {
            var sqlCondition = "ProjectBranchID=@ProjectBranchID AND QuoteProjectBranchID=@QuoteProjectBranchID AND QuoteMetaDataID=@QuoteMetaDataID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, branchId),
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, quoteBranchID),
                this.DB.MakeInParam("@QuoteMetaDataID", TDbDataType.VarChar, metaDataID),
                this.DB.MakeInParam("@QuoteMetaDataVersionID", TDbDataType.VarChar, versionID),
                this.DB.MakeInParam("@LastModifier", TDbDataType.VarChar, modifier),
                this.DB.MakeInParam("@LastModifyTime", TDbDataType.DateTime, DateTime.Now),
            };
            this.DB.ExecSqlStatement(string.Format("Update {0} Set QuoteMetaDataVersionID=@QuoteMetaDataVersionID,LastModifier=@LastModifier,LastModifyTime=@LastModifyTime WHERE {1}", TableName, sqlCondition), paramsList.ToArray());

        }
        public bool IsMetaDataQuoted(string projectBranchID, string metaDataID)
        {
            var sqlCondition = "QuoteProjectBranchID=@QuoteProjectBranchID AND QuoteMetaDataID=@QuoteMetaDataID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, projectBranchID),
                this.DB.MakeInParam("@QuoteMetaDataID", TDbDataType.VarChar, metaDataID)
            };
            var ds = this.DB.ExecuteDataSet(string.Format("SELECT 1 FROM {0} WHERE {1} ", TableName, sqlCondition), paramsList.ToArray());
            bool isQuoted = false;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                isQuoted = true;
            }
            return isQuoted;
        }
        public bool IsExistQuoteMetaData(string projectBranchID, string quoteProjectBranchID) {
            var sqlCondition = "ProjectBranchID=@ProjectBranchID AND QuoteProjectBranchID=@QuoteProjectBranchID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>
            {
                this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchID),
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, quoteProjectBranchID)
            };            
            var ds = this.DB.ExecuteDataSet(string.Format("SELECT 1 FROM {0} WHERE {1} ", TableName, sqlCondition), paramsList.ToArray());
            bool isExist = false;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                isExist = true;
            }
            return isExist;
        }
    }
}
